/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Traffic;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

/**
 *
 * @author Matthew
 */
public class TrafficSimulatorVisualisationPanel extends JPanel{
    View view;

    public void setView(View v){
        view = v;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        //Set the graphical square to be green
        g.setColor(Color.green);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        
        if (view != null)
        {
            //Variables for determining the relation between the model's size and the display panel's size
            int xTranslation = this.getWidth() / (2*Settings.LANE_LENGTH + (Settings.northLanes + Settings.southLanes + 1) * Settings.LANE_WIDTH);
            int yTranslation = this.getHeight() / (2*Settings.LANE_LENGTH + (Settings.eastLanes + Settings.westLanes + 1) * Settings.LANE_WIDTH);
            

            //Draw Roads

            //Set brush colour to dark grey
            g.setColor(Color.DARK_GRAY);

            PositionArea p;

            //For each of the roads/intersection
            for (Zone z:view.areas)
            {
                //draw it
                p = z.getArea();
                g.fillRect(p.x * xTranslation, p.y * yTranslation, p.xLength * xTranslation, p.yLength * yTranslation);
            }

            //Draw traffic lights

            //Northbound traffic light
            p = new PositionArea(Settings.LANE_LENGTH + (Settings.northLanes + Settings.southLanes + 1) * Settings.LANE_WIDTH,
                    Settings.LANE_LENGTH - 3 * Settings.LANE_WIDTH, Settings.LANE_WIDTH * 2, Settings.LANE_WIDTH * 3);

            g.setColor(Color.black);
            g.fillRect(p.x * xTranslation, p.y * yTranslation, p.xLength * xTranslation, p.yLength * yTranslation);

            //Main flow light
            PositionArea p2 = new PositionArea(p);
            //The position and colour of the lit light is dependant on which colour is active
            switch(view.lights[0])
            {
                case RED:
                    g.setColor(Color.red);
                    break;
                case YELLOW:
                    g.setColor(Color.yellow);
                    p2.y += Settings.LANE_WIDTH;
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    p2.y += 2 * Settings.LANE_WIDTH;
                    break;
            }
            p2.xLength = Settings.LANE_WIDTH;
            p2.yLength = Settings.LANE_WIDTH;

            g.fillOval(p2.x * xTranslation, p2.y * yTranslation, p2.xLength * xTranslation, p2.yLength * yTranslation);

            //Turning Light
            switch(view.lights[4])
            {
                case RED:
                    g.setColor(Color.red);
                    p.y += Settings.LANE_WIDTH;
                    break;
                case YELLOW:
                    g.setColor(Color.yellow);
                    p.y += 2 * Settings.LANE_WIDTH;
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    p.y += 3 * Settings.LANE_WIDTH;
                    break;
            }
            p.x += Settings.LANE_WIDTH;

            g.drawString(">", (p.x + (Settings.LANE_WIDTH / 2)) * xTranslation, (p.y - (Settings.LANE_WIDTH / 2)) * yTranslation);

            //Southbound traffic light
            p = new PositionArea(Settings.LANE_LENGTH - 2 * Settings.LANE_WIDTH,
                    Settings.LANE_LENGTH + (Settings.northLanes + Settings.southLanes + 1) * Settings.LANE_WIDTH,
                    Settings.LANE_WIDTH * 2, Settings.LANE_WIDTH * 3);

            g.setColor(Color.black);
            g.fillRect(p.x * xTranslation, p.y * yTranslation, p.xLength * xTranslation, p.yLength * yTranslation);

            //Main flow light
            p2 = new PositionArea(p);
            //The position and colour of the lit light is dependant on which colour is active
            switch(view.lights[1])
            {
                case RED:
                    g.setColor(Color.red);
                    p2.y += Settings.LANE_WIDTH * 2;
                    break;
                case YELLOW:
                    g.setColor(Color.yellow);
                    p2.y += Settings.LANE_WIDTH;
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    break;
            }
            p2.x += Settings.LANE_WIDTH;
            p2.xLength = Settings.LANE_WIDTH;
            p2.yLength = Settings.LANE_WIDTH;

            g.fillOval(p2.x * xTranslation, p2.y * yTranslation, p2.xLength * xTranslation, p2.yLength * yTranslation);

            //Turning Light
            switch(view.lights[5])
            {
                case RED:
                    g.setColor(Color.red);
                    p.y += 3 * Settings.LANE_WIDTH;
                    break;
                case YELLOW:
                    g.setColor(Color.yellow);
                    p.y += 2 * Settings.LANE_WIDTH;
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    p.y += Settings.LANE_WIDTH;
                    break;
            }

            g.drawString("<", (p.x + (Settings.LANE_WIDTH / 2)) * xTranslation, (p.y - (Settings.LANE_WIDTH / 2)) * yTranslation);

            //Eastbound traffic light
            p = new PositionArea(Settings.LANE_LENGTH + (Settings.northLanes + Settings.southLanes + 1) * Settings.LANE_WIDTH,
                    Settings.LANE_LENGTH + (Settings.eastLanes + Settings.westLanes + 1) * Settings.LANE_WIDTH,
                    Settings.LANE_WIDTH * 3, Settings.LANE_WIDTH * 2);

            g.setColor(Color.black);
            g.fillRect(p.x * xTranslation, p.y * yTranslation, p.xLength * xTranslation, p.yLength * yTranslation);

            //Main flow light
            p2 = new PositionArea(p);
            //The position and colour of the lit light is dependant on which colour is active
            switch(view.lights[2])
            {
                case RED:
                    g.setColor(Color.red);
                    p2.x += 2 * Settings.LANE_WIDTH;
                    break;
                case YELLOW:
                    g.setColor(Color.yellow);
                    p2.x += Settings.LANE_WIDTH;
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    break;
            }
            p2.xLength = Settings.LANE_WIDTH;
            p2.yLength = Settings.LANE_WIDTH;

            g.fillOval(p2.x * xTranslation, p2.y * yTranslation, p2.xLength * xTranslation, p2.yLength * yTranslation);

            //Turning Light
            switch(view.lights[6])
            {
                case RED:
                    g.setColor(Color.red);
                    p.x += 2 *Settings.LANE_WIDTH;
                    break;
                case YELLOW:
                    g.setColor(Color.yellow);
                    p.x += 1 * Settings.LANE_WIDTH;
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    break;
            }
            p.y += 2 * Settings.LANE_WIDTH;

            g.drawString("v", (p.x + (Settings.LANE_WIDTH / 2)) * xTranslation, (p.y - (Settings.LANE_WIDTH / 2)) * yTranslation);

            //Westbound traffic light
            p = new PositionArea(Settings.LANE_LENGTH - 3 * Settings.LANE_WIDTH,
                    Settings.LANE_LENGTH - 2 * Settings.LANE_WIDTH, Settings.LANE_WIDTH * 3, Settings.LANE_WIDTH * 2);

            g.setColor(Color.black);
            g.fillRect(p.x * xTranslation, p.y * yTranslation, p.xLength * xTranslation, p.yLength * yTranslation);

            //Main flow light
            p2 = new PositionArea(p);
            //The position and colour of the lit light is dependant on which colour is active
            switch(view.lights[3])
            {
                case RED:
                    g.setColor(Color.red);
                    break;
                case YELLOW:
                    g.setColor(Color.yellow);
                    p2.x += Settings.LANE_WIDTH;
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    p2.x += 2 * Settings.LANE_WIDTH;
                    break;
            }
            p2.y += Settings.LANE_WIDTH;
            p2.xLength = Settings.LANE_WIDTH;
            p2.yLength = Settings.LANE_WIDTH;

            g.fillOval(p2.x * xTranslation, p2.y * yTranslation, p2.xLength * xTranslation, p2.yLength * yTranslation);

            //Turning Light
            switch(view.lights[7])
            {
                case RED:
                    g.setColor(Color.red);
                    break;
                case YELLOW:
                    g.setColor(Color.yellow);
                    p.x += 1 * Settings.LANE_WIDTH;
                    break;
                case GREEN:
                    g.setColor(Color.green);
                    p.x += 2 * Settings.LANE_WIDTH;
                    break;
            }
            p.y += Settings.LANE_WIDTH;

            g.drawString("^", (p.x + (Settings.LANE_WIDTH / 2)) * xTranslation, (p.y) * yTranslation);

            //For each vehicle
            for (Vehicle v:view.vehicles)
            {
                //If vehicle is not turning
                if (v.breakdownDur > 0)
                    g.setColor(Color.green);
                else if (v.turning == VehicleTurn.RIGHT)
                    g.setColor(Color.RED);
                else if (v.turning == VehicleTurn.LEFT)
                    g.setColor(Color.BLUE);
                else
                    g.setColor(Color.BLACK);

                g.fillRect(v.position.x * xTranslation + 1, v.position.y * yTranslation + 1,
                        v.position.xLength * xTranslation - 2, v.position.yLength * yTranslation - 2);
            }
        }
    } 
}
